//
//  LKSearchBar.m
//  YLHelper
//
//  Created by she1323 on 2018/4/16.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKSearchBar.h"
#import "UIImage+Tool.h"
#import "LKDefineFile.h"
@interface LKSearchBar()<UITextFieldDelegate>

@property (nonatomic, assign) CGFloat placeholderWidth;

@end


@implementation LKSearchBar
// placeholder 和icon 和 间隙的整体宽度


// icon宽度
static CGFloat const searchIconW = 10.0;
// icon与placeholder间距
static CGFloat const iconSpacing = 10.0;
// 占位文字的字体大小
static CGFloat const placeHolderFont = 13.0;

- (void)layoutSubviews {
    [super layoutSubviews];
//    // 设置背景图片
//    UIImage *backImage = [UIImage imageWithColor:[UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1]];
//    [self setBackgroundImage:backImage];

    self.backgroundImage = [self imageWithColor:[UIColor clearColor] size:self.bounds.size];
    UITextField  *seachTextFild = [self valueForKey:@"searchField"];
    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel =  object_getIvar(seachTextFild, ivar);
    placeholderLabel.textColor = COLOR_HEX(0xC7C7C7);
    placeholderLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
    UIButton *cancelButton = (UIButton *)[self findViewWithClassName:NSStringFromClass([UIButton class]) inView:self];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
}
/** 取消searchBar背景色 */
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
- (UIView *)findViewWithClassName:(NSString *)className inView:(UIView *)view{
    Class specificView = NSClassFromString(className);
    if ([view isKindOfClass:specificView]) {
        return view;
    }
    
    if (view.subviews.count > 0) {
        for (UIView *subView in view.subviews) {
            UIView *targetView = [self findViewWithClassName:className inView:subView];
            if (targetView != nil) {
                return targetView;
            }
        }
    }
    
    return nil;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
